using Cysharp.Threading.Tasks;
using IQIGame.Onigao.Framework;
using IQIGame.Onigao.Game;
using System;
using UnityEngine;

namespace IQIGame.Onigao.GamePlay
{
    /// <summary>
    /// 跟随npc玩法容器，处理具体业务逻辑
    /// 不需要再继承LevelPlaySubBaseCtrl或TController
    /// 当然想继承也行，但在玩法结束时需要从父Controller中移除销毁
    /// </summary>
    public class LevelNpcTailingContainer : IDisposable
    {
        #region fields
        private ResLoader _resLoader;
        private GameObject _mainAsset;
        #endregion fields

        #region auto generated binding
        public UnityEngine.GameObject target { protected set; get; }
        public virtual void InitBinding(ObjectBinding __binding)
        {
            __binding.TryGetVariableValue<UnityEngine.GameObject>("target", out var __tbv0);
            this.target = __tbv0;
        }
        #endregion auto generated binding

        public LevelNpcTailingContainer()
        {
            _resLoader = ResLoader.Create();
        }

        public async UniTask Start(int cid)
        {
            //加载具体资源（正式情况根据cid取配置数据）
            var asset = await _resLoader.LoadABAssetAwait<GameObject>(PathConstant.GetMiniGameAsset(GameplayAreaType.Level, "NpcTailing/NpcTailing1"));
            _mainAsset = UnityEngine.Object.Instantiate(asset);
            //绑定公共数据，如target（被跟踪的npc）
            InitBinding(_mainAsset.GetComponent<ObjectBinding>());

            //动态加载资源都必须通过_resLoader完成，不要再创建新的ResLoader

            //处理业务
        }

        /// <summary>
        /// 帧刷
        /// </summary>
        /// <param name="deltaTime"></param>
        public void OnUpdate(float deltaTime)
        {

        }

        public void Dispose()
        {
            UnityEngine.Object.Destroy(_mainAsset);
            _mainAsset = null;
            _resLoader?.Put2Pool();
            _resLoader = null;
        }
    }
}
